﻿using System;

namespace DotNetRepository.Cache
{
    internal class CacheItem<TKey, TItem>
        where TKey : struct
        where TItem : class
    {
        internal CacheItem(TItem value, CacheItemPriority priority, ICacheItemExpiration<TKey,TItem> expiration,
            ICacheItemRefreshAction<TKey,TItem> refreshAction)
        {
            Value = value;
            Priority = priority;
            Expiration = expiration;
            RefreshAction = refreshAction;
            Added = DateTime.Now;
            LastAccessedTime = Added;
            Hits = 0;

            Expiration.Initialize(this);
        }
        
        internal TItem Value { get; private set;}
        internal CacheItemPriority Priority {get; private set;}
        internal DateTime Added { get; private set; }
        internal ICacheItemExpiration<TKey,TItem> Expiration { get; private set;}
        internal ICacheItemRefreshAction<TKey,TItem> RefreshAction { get; private set;}
        internal void Refresh(TKey key, TItem item, CacheItemRemovedReason expiredReason)
        {
            if (RefreshAction != null)
                RefreshAction.Refresh(key, item, expiredReason);
        }

        internal DateTime LastAccessedTime { get; set; }
        internal int Hits { get; set; }
        
    }
}
